package org.example.base.websocket.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import org.example.base.common.model.TUserRoleModel;
import org.example.base.common.model.UserModel;
import org.example.base.common.service.TRoleService;
import org.example.base.common.service.TUserRoleService;
import org.example.base.common.service.UserService;
import org.example.base.websocket.dao.WebSocketDao;
import org.example.base.websocket.service.WebSocketService;
import org.example.base.websocket.model.ContactModel;
import org.example.core.mysql.Where;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/1/20 01:31
 */
@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    private WebSocketDao webSocketDao;

    @Autowired
    private TUserRoleService tUserRoleService;

    @Autowired
    private UserService userService;



    @Override
    public void readMessage(String currentUserId, String toConnectUserId) {
        webSocketDao.readMessage(currentUserId, toConnectUserId);
    }

    @Override
    public List<Map<String,Object>> getContactList(String currentUserId) {
        TUserRoleModel tUserRoleModel = tUserRoleService.searchOne(Where.build().field("role_id").equal("base-role")
                .and().field("user_id").equal(currentUserId));
        if (ObjectUtils.isEmpty(tUserRoleModel)){
            //普通用户
            List<TUserRoleModel> tUserRoleModelList = tUserRoleService.search(Where.build().field("role_id").equal("base-role"));
            List<String> userIds = tUserRoleModelList.stream().map(TUserRoleModel::getUserId).distinct().collect(Collectors.toList());
            List<UserModel> userModelList = userService.search(Where.build().field("id").in(userIds));
            List<Map<String,Object>> result = new ArrayList<>();
            userModelList.stream().forEach(userModel -> {
                Map<String,Object> resultMap = webSocketDao.getContactList(currentUserId,userModel.getId());
                if (MapUtil.isEmpty(resultMap)){
                    resultMap = new HashMap<>();
                }
                resultMap.put("id",userModel.getId());
                resultMap.put("avatar",userModel.getImg());
                resultMap.put("displayName",userModel.getName());
                result.add(resultMap);
            });
            return result;
        }else {
            //系统管理员
            List<TUserRoleModel> tUserRoleModelList = tUserRoleService.search(Where.build().field("role_id").notEqual("base-role"));
            List<String> userIds = tUserRoleModelList.stream().map(TUserRoleModel::getUserId).distinct().collect(Collectors.toList());
            List<UserModel> userModelList = userService.search(Where.build().field("id").in(userIds));
            List<Map<String,Object>> result = new ArrayList<>();
            userModelList.stream().forEach(userModel -> {
                Map<String,Object> resultMap = webSocketDao.getContactList(currentUserId,userModel.getId());
                if (MapUtil.isEmpty(resultMap)){
                    return;
                }
                resultMap.put("id",userModel.getId());
                resultMap.put("avatar",userModel.getImg());
                resultMap.put("displayName",userModel.getName());
                result.add(resultMap);
            });
            return result;
        }
    }

    @Override
    public List<Map<String, Object>> getRecentChatMsg(String currentUserId, String toConnectUserId) {
        List<Map<String, Object>> recentChatMsg = webSocketDao.getRecentChatMsg(currentUserId, toConnectUserId);
        return recentChatMsg.stream().map(map -> {
            Object userId = map.get("userId");
            UserModel userModel = userService.getById((String) userId);
            JSONObject fromUser = new JSONObject();
            fromUser.put("id",userId);
            fromUser.put("displayName",userModel.getName());
            fromUser.put("avatar",userModel.getImg());
            map.put("fromUser",fromUser);
            return map;
        }).collect(Collectors.toList());

    }
}
